import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home.vue'
import store from "@/store"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "home"
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home.vue')
  },
  {
    path: '/goods',
    name: 'hogoodsme',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    // 路由独享守卫
    // beforeEnter: ((to, from, next) => {
    //   let token = localStorage.getItem('x-auth-token');
    //   if (token) {
    //     next()
    //   } else {
    //     store.dispatch('ShowToast/asyncToast', {
    //       showToast: true,
    //       text: '登录后才能访问哦~~',
    //       type: 'danger'
    //     })
    //   }
    // })
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/Details.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 需求：切换到个人中心需要 登录才可以

// 全局前置守卫
/* router.beforeEach((to, from, next) => {
  if (to.path == "/user") {
    let token = localStorage.getItem('x-auth-token');
    if (token) {
      next()
    } else {
      store.dispatch('ShowToast/asyncToast', {
        showToast: true,
        text: '登录后才能访问！！！',
        type: 'danger'
      })
    }
  } else {
    next()
  }
}) */

// 解决重复点击 路由错误
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};
export default router
